package com.kunbo.service;

import com.kunbo.entity.Bearing;
import com.kunbo.entity.LineChart;
import com.kunbo.mapper.BearingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BearingService {

    @Autowired
    private BearingMapper bearingMapper;

    //得到x向的数据
    public List<Bearing> getX(){
        return bearingMapper.selectBearingX();
    }

    //得到y向的数据
    public List<Bearing> getY(){
        return bearingMapper.selectBearingY();
    }

    //得到z向的数据
    public List<Bearing> getZ(){
        return bearingMapper.selectBearingZ();
    }

    //得到全部的数据
    public List<Bearing> getAll(){
        return bearingMapper.selectBearingAll();
    }

    //输出数据到控制层
    public LineChart sendALL(){//返回条形图对象
        List<Bearing> bearingAll = getAll();//取到bearing的list
        List csys = new ArrayList();//time轴
        List x = new ArrayList();//x的数据
        List y = new ArrayList();//y的数据
        List z = new ArrayList();//z的数据
        for (Bearing bearing : bearingAll){
            csys.add(bearing.getTime());
            x.add(bearing.getX());
            y.add(bearing.getY());
            z.add(bearing.getZ());
        }

        //条形图实体类
        LineChart lineChart = new LineChart();
        //实体类的内部类
            LineChart.time time = lineChart.new time();
            LineChart.data data = lineChart.new data();
            LineChart.data data1 = lineChart.new data();
            LineChart.data data2 = lineChart.new data();
            LineChart.type type1 = lineChart.new type();
            LineChart.type type2 = lineChart.new type();
            LineChart.type type3 = lineChart.new type();
            LineChart.realDate realDate = lineChart.new realDate();
            LineChart.realDate realDate2 = lineChart.new realDate();
            LineChart.realDate realDate3 = lineChart.new realDate();
        //存入“common“
        time.setMonth(csys);//时间的存入
        lineChart.setCommon(time);
        List<LineChart.realDate> lineX = new ArrayList<>();
        List<LineChart.realDate> lineY = new ArrayList<>();
        List<LineChart.realDate> lineZ = new ArrayList<>();
        //数据注入
        realDate.setName("x");
        realDate.setData(x);
        realDate2.setName("y");
        realDate2.setData(y);
        realDate3.setName("z");
        realDate3.setData(z);
        lineX.add(realDate);
        lineY.add(realDate2);
        lineZ.add(realDate3);
        //标题注入
        data.setTitle("振动数据x");
        data.setData(lineX);
        data1.setTitle("振动数据y");
        data1.setData(lineY);
        data2.setTitle("振动数据z");
        data2.setData(lineZ);
        type1.setKey("map");
        type1.setText("振动数据x");
        type2.setKey("seller");
        type2.setText("振动数据y");
        type3.setKey("commodity");
        type3.setText("振动数据z");

        List<LineChart.type> type = new ArrayList<>();
        type.add(type1);
        type.add(type2);
        type.add(type3);
        lineChart.setType(type);
        lineChart.setMap(data);
        lineChart.setSeller(data1);
        lineChart.setCommodity(data2);

        return lineChart;
    }
}
